package com.huayue.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.BaseDao;
import com.huayue.dao.CustomerDao;
import com.huayue.dao.RoomPredictOrderDao;
import com.huayue.dao.RoomPredictOrderDetailsDao;
import com.huayue.entity.Customer;
import com.huayue.entity.RoomPredictOrder;
import com.huayue.entity.RoomPredictOrderDetails;
import com.huayue.entity.RoomStatus;
import com.huayue.util.DateHelper;

/**
 * 预订单
 */
@Service
public class RoomPredictOrderService extends BaseService<RoomPredictOrder> {

    @Autowired
    RoomPredictOrderDao dao;

    @Autowired
    RoomPredictOrderDetailsDao detailsDao;
    @Autowired
    CustomerDao cusDao;

    @Override
    public BaseDao<RoomPredictOrder> getDao() {
        return dao;
    }

    // 查询空房间
    public List<Map<String, Object>> findNullfrom(String checkDate, Integer days) {
        return dao.findAvailRoom(checkDate, days);
    }

    /**
     * 添加订单信息
     *
     * @param orderInfo
     * @param details
     * @return
     */
    public boolean addDetails(RoomPredictOrder orderInfo, List<RoomPredictOrderDetails> details, Customer customer) {
        // 1.添加顾客信息
        if (cusDao.insert(customer) == 0) {
            System.out.println("添加顾客信息失败");
            return false;
        }
        // 2.添加订单信息
        int success = 0;
        // 获取总预定天数
        int day = DateHelper.getDateRangeNumber(orderInfo.getPredictInTime(), orderInfo.getPredictOutTime());
        if (day == 0) {
            day = 1;
        }
        orderInfo.setDays(Long.valueOf(day));
        // 计算总钱数
        BigDecimal price = new BigDecimal("0");
        for (RoomPredictOrderDetails orderDetails : details) {
            price = price.add(orderDetails.getPrice());
        }
        price = price.multiply(BigDecimal.valueOf(day));
        orderInfo.setTotalPrice(price);
        // 完善订单信息
        // 将订单信息添加到数据库
        success = dao.insert(orderInfo);

        // 判断是否添加成功
        // if (success == 1) {
        // // 将订单详情添加到数据库
        // for (RoomPredictOrderDetails orderDetails : details) {
        // success += detailsDao.insert(orderDetails);
        // dao.updateRoomStatus(orderDetails.getRoomId());
        // }
        // }
        if (success == 1) {
            // 将订单详情添加到数据库
            for (RoomPredictOrderDetails orderDetails : details) {
                success += detailsDao.insert(orderDetails);
                RoomStatus roomStatus = new RoomStatus();
                roomStatus.setId(orderDetails.getRoomId());
                roomStatus.setInTime(orderInfo.getPredictInTime());
                roomStatus.setOutTime(orderInfo.getPredictOutTime());
                dao.updateRoomStatus(roomStatus);
            }
        }
        if (success == (details.size() + 1)) {
            return true;
        }
        return false;
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    public List<Map<String, Object>> findDetails(String id) {
        List<Map<String, Object>> details = detailsDao.findDetails(id);
        List<Map<String, Object>> finalDetails = new ArrayList<Map<String, Object>>();
        // 处理向前台发送数据
        for (Map<String, Object> map : details) {
            Map<String, Object> newMap = new HashMap<String, Object>();
            for (int i = 0; i < map.size(); i++) {
                newMap.put("roomTypeId", map.get("room_type_id"));
                newMap.put("roomId", map.get("room_id"));
                newMap.put("oneDayPrice", map.get("rtprice"));
                newMap.put("price", map.get("price"));
            }
            finalDetails.add(newMap);
        }

        return finalDetails;
    }

    /**
     * 根据房间ID查询预定信息
     *
     * @return
     */
    public Map<String, Object> findPredictByRoomId(String id) {
        return dao.findPredictByRoomId(id);
    }

    /**
     * 根据电话查未入住的预定单
     * 
     * @param phone
     * @return
     */
    public List<Map<String, Object>> findPredictOrderByPhone(String phone) {
        return dao.findPredictOrderByPhone(phone);
    }

    public int updatePredictOrderStatus(Integer roomId) {
        return dao.updatePredictOrderStatus(roomId);
    }

    /**
     * 定时将已预定但时间到期的房间状态更改为空房
     */
    public int updatePredictOrderNotCheckIn() {
        return dao.updatePredictOrderNotCheckInAUTO();
    }

    /**
     * 定时将已预定的房间信息更新到房态表中
     *
     * @return
     */
    public int updateRoomStatusByPredictAUTO() {
        return dao.updateRoomStatusByPredictAUTO();
    }
}
